<?php
  include_once('./queries/utils.inc.php');

  function single_proc_matchmaking_history_query($connection, 
           $index, $schedd, $cid, $pid, $matchTime, $pn) {

    $date = TS_FORMAT;
    $sql = "SELECT to_char(m.match_time, '{$date}'), m.username, " . 
           "m.machine_id, m.remote_user, m.remote_priority " .
           "FROM matches m, history_horizontal hh " . 
           "WHERE hh.scheddname='{$schedd}' " .
              "AND hh.cluster_id='{$cid}' ". 
              "AND hh.proc_id='{$pid}' " .
              "AND to_char(m.match_time, '{$date}')='{$matchTime}' " .
              "AND m.scheddname=hh.scheddname " .
              "AND m.cluster_id=hh.cluster_id " .
              "AND m.proc_id=hh.proc_id ";
              if (!($_SESSION["administrator"] === true)) {
	        $sql = $sql . "AND hh.owner = '{$_SESSION["loginUsername"]}' ";
              }

   $rcount = get_recordcount($connection, $sql);
	 $sql = $sql . "ORDER BY m.match_time desc ";
   $sql = do_pagination_rewriting($pn, $rcount, $sql);

    //echo $sql;
    unset($_SESSION["{$index}"]);    
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Historical information about matches for job {$schedd}.{$cid}.{$pid}.";
    $_SESSION["{$index}"]["message"]="Historical information about matches for job {$schedd}.{$cid}.{$pid}.";

    $fields=array();
    $fields[0]="match_time";
    $fields[1]="username";
    $fields[2]="machine_id";
    $fields[3]="remote_user";
    $fields[4]="remote_priority";

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function single_proc_matchmaking_query($connection, $index, $schedd, 
           $cid, $pid, $matchTime) {

    $date = TS_FORMAT;
    $sql = "SELECT to_char(m.match_time, '{$date}') AS match_time, " .
           "m.username, m.machine_id, m.remote_user, m.remote_priority " .
           "FROM matches m, clusterads_horizontal c " . 
           "WHERE m.scheddname='{$schedd}' " .
              "AND m.cluster_id='{$cid}' ". 
              "AND m.proc_id='{$pid}' " .
              "AND to_char(m.match_time, '{$date}')='{$matchTime}' " .
              "AND m.scheddname=c.scheddname " .
              "AND m.cluster_id=c.cluster_id ";
              if (!($_SESSION["administrator"] === true)) { 
	        $sql = $sql . "AND c.owner = '{$_SESSION["loginUsername"]}' ";
              }
    $rcount = get_recordcount($connection, $sql);
    $sql = $sql . "ORDER BY match_time desc ";
    $sql = do_pagination_rewriting(1, $rcount, $sql);

    //echo $sql;
    unset($_SESSION["{$index}"]);    
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Current information about matches for job {$schedd}.{$cid}.{$pid}.";
    $_SESSION["{$index}"]["message"]="Current information about matches for job {$schedd}.{$cid}.{$pid}.";

    $_SESSION["{$index}"]["recSet"] = get_recordset($connection, $sql);
    $_SESSION["{$index}"]["time"] = date("r", time());

    $fields=array();
    $fields[0]="match_time";
    $fields[1]="username";
    $fields[2]="machine_id";
    $fields[3]="remote_user";
    $fields[4]="remote_priority";

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function single_proc_rejectmaking_history_query($connection, $index, 
           $schedd, $cid, $pid, $rejectTime, $pn) {
    
    $date = TS_FORMAT;
    $sql = "SELECT to_char(r.reject_time, '{$date}') AS reject_time, " .
           "r.username, r.startdname, r.diagnosis " . 
           "FROM rejects r, jobs_horizontal_history hh " . 
           "WHERE hh.scheddname='{$schedd}' " .
              "AND hh.cluster_id='{$cid}' ". 
              "AND hh.proc_id='{$pid}' " .
              "AND to_char(r.reject_time, '{$date}')='{$rejectTime}' " .
              "AND r.scheddname=hh.scheddname " .
              "AND r.cluster_id=hh.cluster_id " .
              "AND r.proc_id=hh.proc_id ";
              if (!($_SESSION["administrator"] === true)) {
				  $sql = $sql . "AND hh.owner = '{$_SESSION["loginUsername"]}' ";
              }
    $rcount = get_recordcount($connection, $sql);
    $sql = $sql . "ORDER BY reject_time desc ";
    $sql = do_pagination_rewriting($pn, $rcount, $sql);
			  
    unset($_SESSION["{$index}"]);    
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Historical information about rejects for job {$schedd}.{$cid}.{$pid}.";
    $_SESSION["{$index}"]["message"]="Historical information about rejects for job {$schedd}.{$cid}.{$pid}.";

    $fields=array();
    $fields[0]="reject_time";
    $fields[1]="username";
    $fields[2]="startdname";
    $fields[3]="diagnosis";

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }

  function single_proc_rejectmaking_query($connection, $index, 
           $schedd, $cid, $pid, $rejectTime, $pn) {
    
    $date = TS_FORMAT;
    $sql = "SELECT to_char(r.reject_time, '{$date}') AS reject_time, " .
           "r.username, r.startdname, r.diagnosis " . 
           "FROM rejects r, clusterads_horizontal c " . 
           "WHERE r.scheddname='{$schedd}' " .
              "AND r.cluster_id='{$cid}' ". 
              "AND r.proc_id='{$pid}' " .
              "AND to_char(r.reject_time, '{$date}')='{$rejectTime}' " .
              "AND r.scheddname=c.scheddname " .
              "AND r.cluster_id=c.cluster_id ";
              if (!($_SESSION["administrator"] === true)) { 
	        $sql = $sql . "AND c.owner = '{$_SESSION["loginUsername"]}' ";
              }
    $rcount = get_recordcount($connection, $sql);
    $sql = $sql . "ORDER BY reject_time desc ";
    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    unset($_SESSION["{$index}"]);    
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Current information about rejects for job {$schedd}.{$cid}.{$pid}.";
    $_SESSION["{$index}"]["message"]="Current information about rejects for job {$schedd}.{$cid}.{$pid}.";

    $fields=array();
    $fields[0]="reject_time";
    $fields[1]="username";
    $fields[2]="startdname";
    $fields[3]="diagnosis";

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }
?>
